package com.it.commicity.controller.sys;


import com.it.commicity.common.Result;
import com.it.commicity.common.ResultCodeEnum;
import com.it.commicity.domain.po.SysUser;
import com.it.commicity.domain.requestPass.Password;
import com.it.commicity.domain.vo.SysUserVo;
import com.it.commicity.domain.vo.user.SysUserDto;
import com.it.commicity.service.sys.ISysUserService;
import com.it.commicity.utils.UserHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-07-11
 */
@RestController
@RequestMapping("/api")
public class SysUserController {
    @Resource
    private ISysUserService sysUserService;

    /**
     * 登录
     *
     * @param sysUser
     * @return
     */
//    @CrossOrigin
    @PostMapping("/sys/login")
    public Result<SysUserVo> login(@RequestBody SysUser sysUser) {
        return sysUserService.login(sysUser);
    }

    @GetMapping("/sys/queryById")
    public Result<SysUserDto> queryById(Long userId) {
        if (!userId.equals(UserHolder.getUser().getUserId())) {
            return Result.fail(ResultCodeEnum.TOKEN_INVALID);
        }
        SysUserDto sysUser = sysUserService.queryByUserId(userId);
        if (sysUser == null) {
            System.err.println("SysUserController->" + sysUser);
            return Result.fail(ResultCodeEnum.DATA_ERROR);
        }
        return Result.success(sysUser);
    }

    @PutMapping("/sys/updateUserInfo")
    public Result updateUserByUserId(@RequestBody SysUser user) {
        return sysUserService.updateUserByUserId(user);
    }

    @PutMapping("sys/updataPassword")
    public Result updateUserPasswordByUserId(@RequestBody Password password) {
        System.out.println("SysUserController-updateUserPasswordByUserId->更新密码" + password);
        return sysUserService.updateUserPasswordByUserId(password);
    }
}
